//
//  LYCollectionViewController.m
//  LYKitDemo
//
//  Created by Mac-Mini-KingVision on 16/1/26.
//  Copyright © 2016年 LiuWenYang. All rights reserved.
//

#import "LYCollectionViewController.h"
#import <MJRefresh.h>
#import <ReactiveCocoa.h>
#import "CollectionViewCell.h"

@interface LYCollectionViewController ()<CollectionViewItemDelegate>


@end

@implementation LYCollectionViewController

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return self.dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.classForCellString forIndexPath:indexPath];
    [cell setValue:[_dataSource objectAtIndex:indexPath.row]];
    cell.itemDelegate = self;
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.calculateSize) {
        id data = [_dataSource objectAtIndex:indexPath.row];
        Class cellClass = NSClassFromString(self.classForCellString);
        return [cellClass sizeForItem:data indexPath:indexPath];
    }
    
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)collectionViewLayout;
    
    CGRect rect = [UIScreen mainScreen].bounds;
    
    CGFloat sectionInsetWidth = layout.sectionInset.left + layout.sectionInset.right;
    
    CGFloat minimumInteritemSpacings = (self.column - 1) * layout.minimumInteritemSpacing;
    
    CGFloat width = (rect.size.width - sectionInsetWidth - minimumInteritemSpacings) / self.column;
    
    return CGSizeMake(width, layout.itemSize.height);
}

-(void)setCollectionView:(UICollectionView *)collectionView
{
    _collectionView = collectionView;
//    Class cellClass = NSClassFromString(self.classForCellString);
//    [_collectionView registerClass:cellClass forCellWithReuseIdentifier:self.classForCellString];
    if (self.hasRefreshHeader) {
        self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
    }
    if (self.hasLoadMoreFooter) {
        self.collectionView.mj_footer  = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - 顶部的重新刷新
#pragma mark RefreshHeaderViewDelegate

- (void)refreshHeaderViewDidCallBack
{
    [self.collectionView.mj_header beginRefreshing];
    self.pageIndex = 1;
    __weak LYCollectionViewController  *weakSelf = self;
    [self refreshTable:^(BOOL finished) {
        if (finished) {
            [weakSelf.collectionView.mj_header endRefreshing];
            
            [self configureHeaderAndFooter];
        } else {
            [weakSelf.collectionView.mj_header endRefreshing];
            NSLog(@"modelDidLoadFailure");
        }
    }];
}

#pragma mark - 底部的获取更多
#pragma mark RefreshFooterViewDelegate

- (void)refreshFooterViewDidCallBack
{
    [self.collectionView.mj_footer beginRefreshing];
    __weak LYCollectionViewController *weakSelf = self;
    ++self.pageIndex;
    [self getTableMore:^(BOOL finished) {
        if (finished) {
            [weakSelf.collectionView.mj_footer endRefreshing];
        }
        [self configureHeaderAndFooter];
    }];
}

-(void)forceRefreshWithoutAnimation
{
    @weakify(self);
    self.pageIndex = 1;
    [self refreshTable:^(BOOL finished) {
        @strongify(self);
        [self configureHeaderAndFooter];
    }];
}

-(void)configureHeaderAndFooter
{
    if (self.totalCount > self.dataSource.count) {
        //需要展现加载更多
        self.collectionView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
    }else{
        [self.collectionView.mj_footer removeFromSuperview];
//        self.tableView.tableFooterView = [[UIView alloc] init];
    }
}

- (void)refreshTable:(void (^)(BOOL))block
{
    //virtual method
}

- (void)getTableMore:(void (^)(BOOL))block
{
    //virtual method
}

@end
